Skip to content

Method: add(Descriptor, Object, Object)

1: /*
2: * JOPA
3: * Copyright (C) 2024 Czech Technical University in Prague
4: *
5: * This library is free software; you can redistribute it and/or
6: * modify it under the terms of the GNU Lesser General Public
7: * License as published by the Free Software Foundation; either
8: * version 3.0 of the License, or (at your option) any later version.
9: *
10: * This library is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13: * Lesser General Public License for more details.
14: *
15: * You should have received a copy of the GNU Lesser General Public
16: * License along with this library.
17: */
18: package cz.cvut.kbss.jopa.sessions;
19:
20: import cz.cvut.kbss.jopa.model.descriptors.Descriptor;
21:
22: import java.net.URI;
23: import java.util.HashMap;
24: import java.util.IdentityHashMap;
25: import java.util.Map;
26: import java.util.Set;
27:
28: final class RepositoryMap {
29:
30: private final Map<Set<URI>, Map<Object, Object>> origsToClones = new HashMap<>();
31: private Map<Object, Descriptor> entityDescriptors;
32:
33: RepositoryMap() {
34: initDescriptors();
35: }
36:
37: void initDescriptors() {
38: this.entityDescriptors = new IdentityHashMap<>();
39: }
40:
41: void add(Descriptor descriptor, Object original, Object clone) {
42:• assert descriptor != null;
43:• assert original != null;
44: // Null values are permitted
45:
46: final Map<Object, Object> entities = getMap(descriptor);
47: entities.put(original, clone);
48: }
49:
50: void remove(Descriptor descriptor, Object original) {
51: assert descriptor != null;
52: assert original != null;
53:
54: final Map<Object, Object> entities = getMap(descriptor);
55: entities.remove(original);
56: }
57:
58: void addEntityToRepository(Object entity, Descriptor descriptor) {
59: assert entityDescriptors != null;
60: entityDescriptors.put(entity, descriptor);
61: }
62:
63: void removeEntityToRepository(Object entity) {
64: assert entityDescriptors != null;
65: entityDescriptors.remove(entity);
66: }
67:
68: boolean contains(Descriptor descriptor, Object original) {
69: assert descriptor != null;
70: assert original != null;
71:
72: final Map<Object, Object> entities = getMap(descriptor);
73: return entities.containsKey(original);
74: }
75:
76: Object get(Descriptor descriptor, Object original) {
77: assert descriptor != null;
78: assert original != null;
79:
80: final Map<Object, Object> entities = getMap(descriptor);
81: if (!entities.containsKey(original)) {
82: return null;
83: }
84: return entities.get(original);
85: }
86:
87: Descriptor getEntityDescriptor(Object entity) {
88: assert entityDescriptors != null;
89: assert entity != null;
90:
91: return entityDescriptors.get(entity);
92: }
93:
94: void clear() {
95: origsToClones.values().forEach(Map::clear);
96: initDescriptors();
97: }
98:
99: private Map<Object, Object> getMap(Descriptor descriptor) {
100: final Set<URI> ctx = descriptor.getContexts();
101: Map<Object, Object> entities;
102: if (!origsToClones.containsKey(ctx)) {
103: entities = new IdentityHashMap<>();
104: origsToClones.put(ctx, entities);
105: } else {
106: entities = origsToClones.get(ctx);
107: }
108: return entities;
109: }
110: }